//TODO liuguo
//if (top.Theme) {
//    document.write('<link href="tree-1.css"/*tpa=http://115.28.37.205:801/css/' + top.Theme + '/tree.css*/ rel="stylesheet" type="text/css" />');
//}
//else {
//    document.write('<link href="GetCSS-tree-1.css"/*tpa=http://115.28.37.205:801/GetCSS?tree.css*/ rel="stylesheet" type="text/css" />');
//}

function AddTreeView(container, id) {
    var tree;
    if (!window[id]) {
        if (container) {
            tree = new TreeView(id);
            container.appendChild(tree);
        }
    } else {
        tree = window[id];
    }
    return tree;
}

var TreeView = function (id) {
    var tree = window[id];
    if (!tree) {
        tree = document.createElement("ul");
        tree.className = "subNav";
        tree.id = id;
        tree.AllNodes = {};
        tree.Nodes = [];
        tree.DefaultState = "Collapse";
        tree.MultiSelect = false;
        tree.SelectedNodes = [];
        tree.CheckedNodes = [];

        tree.ClearNodes = function () {
            for (var n in tree.Nodes) {
                tree.removeChild(tree.Nodes[n]);
            }
        };

        tree.AddNodes = function (id, text, pid, icon, url) {
            var liNode = document.createElement("li");
            liNode.id = id;
            liNode.Nodes = new Array();
            liNode.Checked = false;
            liNode.Text = text;
            liNode.State = tree.DefaultState;
            liNode.Tree = tree;

            var iTagMark = document.createElement("i");
            iTagMark.Node = liNode;
            iTagMark.title = text;
            if (icon) {
                if(icon!='ztree_file'){
                    iTagMark.className ="fa "+ icon;
                }else{
                    iTagMark.className = "fa fa-circle-o";
                }

            } else {
                if (pid == "" || pid == null) {
                    iTagMark.className = "fa fa-th";
                } else {
                    iTagMark.className = "fa fa-circle-o";
                }
            }
            liNode.Mark = iTagMark;

            var aLabel = document.createElement("a");
            aLabel.Node = liNode;
            if (url != "" && null != url) {
                aLabel.setAttribute("href", "javascript:void(0)");
                aLabel.setAttribute("onclick", "javascript:void(main.location.href='"+url+"')");
                //aLabel.setAttribute("target", "main");
            }
            //aLabel.appendChild(iTagMark);
            liNode.Label = aLabel;
            var spanLabel = document.createTextNode(text);
            //document.createElement("span");
            liNode.Cpation = spanLabel;
            //spanLabel.innerHTML = text;
            aLabel.appendChild(spanLabel);
            liNode.appendChild(iTagMark);
            liNode.appendChild(aLabel);
            liNode.setText = function (txt) {
                liNode.Caption.innerHTML = txt;
            };
            var pnode;
            if (pid) {
                pnode = tree.AllNodes[pid];
            }
            if (pnode) {
                pnode.Nodes.push(liNode);
                tree.AllNodes[id] = liNode;
                var cnodes = pnode.ChildNodes;
                if (!cnodes) {
                    var iTagMark2 = document.createElement("i");
                    iTagMark2.className = "fa fa-angle-left pull-right";
                    $("#"+pnode.id).find("a").append(iTagMark2);
                    //console.log(pnode);

                    cnodes = document.createElement("ul");
                    cnodes.className = "treeview-menu";

                    pnode.className = "treeview";
                    pnode.appendChild(cnodes);
                    pnode.ChildNodes = cnodes;
                }
                cnodes.appendChild(liNode);
                liNode.Index = pnode.Nodes.length - 1;
                liNode.Parent = pnode;
                liNode.Level = pnode.Level + 1;
            } else {
                tree.AllNodes[id] = liNode;
                tree.Nodes.push(liNode);
                liNode.Parent = tree;
                tree.appendChild(liNode);
                liNode.Level = 1;
                liNode.Index = tree.Nodes.length - 1;
            }

            return liNode;
        };

        tree.InitTree = function (treeinfo, treedefine) {
            if (!treedefine) {
                treedefine = {Indx: "Indx", Caption: "Caption", Icon: "Icon", Url: "URL", Parent: "Parent"};
            }
            //Parent":"pid","Caption":"menuName","Url":"url","Indx":"indx","Icon":"menuIcon"
            for (var i = 0; i < treeinfo.length; i++) {
                var nodeDefine = treeinfo[i];
                var nodeItem = tree.AddNodes(nodeDefine[treedefine.Indx], nodeDefine[treedefine.Caption],
                    nodeDefine[treedefine.Parent], nodeDefine[treedefine.Icon], nodeDefine[treedefine.Url]);
                nodeItem.TreeDefine = treedefine;
                nodeItem.NodeData = nodeDefine;
            }
        };
        window[id] = tree;
    }
    return tree;
};
